<?php

namespace App\Http\Controllers\Admin;
use App\DTOS\Admin\UserMemberDTO;
use App\Http\Controllers\Controller;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;

class StallController extends Controller
{

    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;

    /**
     * StallController constructor.
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }

    public function paginate()
    {
        global $gameArea;
        $memberName = request()->get('memberName');
        $searchParams = request()->all();
        if ($memberName){
            $userMemberPaginate = $this->userMemberService->paginate(new UserMemberDTO(['username'=>$memberName]));
            $members = $userMemberPaginate->items();
            if (empty($members)){
                return ResponseUtil::error("没有数据");
            }
            $searchParams['memberIds'] = collect($members)->pluck('id')->unique()->toArray();
        }
        $result = callRpc('/stallService/paginate',$searchParams,['gameArea'=>$gameArea]);
        if (empty($result)){
            return ResponseUtil::error("没有数据");
        }
        $data = $result['data'];
        $userIds = collect($data)->pluck('user_id')->unique()->toArray();
        $members = $this->userMemberService->getByIds($userIds);
        foreach($data as &$item){
            $member = collect($members)->where('id',$item['user_id'])->first();
            $item['memberName'] = data_get($member,'username');
            $sell_item_list = json_decode($item['sell_item_list'],true);
            if (is_array($sell_item_list['data'])){
                $sellItemListData = $sell_item_list['data'];
                foreach($sellItemListData as &$item1){
                    $item1['base']['uniqueid'] = (string)$item1['base']['uniqueid'];
                }
                $sell_item_list['data'] = $sellItemListData;
            }
            $item['sell_item_list'] = $sell_item_list;
        }
        $res = [];
        $res['code'] = 0;
        $res['count'] = $result['total'];
        $res['data'] = $data;
        return ResponseUtil::json($res);
    }

}
